package com.jf.docsify.common.exception;

import com.jf.docsify.common.utils.Resp;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/8/3.1       chenwr    2022/8/3           Create
 * </pre>
 * @date 2022/8/3
 */
public class DocsifyException extends RuntimeException {

    private String msg;

    private int code;

    protected DocsifyException(String msg) {
        this(Resp.ERROR, msg);
    }

    protected DocsifyException(int errCode, String msg) {
        super(msg);
        this.code = errCode;
        this.msg = msg;
    }

    protected DocsifyException(Throwable cause) {
        this(Resp.ERROR, cause);
    }

    protected DocsifyException(int errCode, Throwable cause) {
        super(cause);
        this.code = errCode;
        if (isInstanceof(cause)) {
            this.msg = ((DocsifyException) cause).msg;
        }
    }

    protected DocsifyException(String message, Throwable cause) {
        this(Resp.ERROR, message, cause);
    }

    protected DocsifyException(int errCode, String message, Throwable cause) {
        super(message, cause);
        this.code = errCode;
        this.msg = message;
    }

    public static void throwE(int errCode, String msg) {
        throw new DocsifyException(errCode, msg);
    }

    public static void throwE(String msg) {
        throw new DocsifyException(msg);
    }

    public static void throwE(int errCode, String msg, Throwable cause) {
        throw new DocsifyException(errCode, msg, cause);
    }

    public static void throwE(String msg, Throwable cause) {
        throw new DocsifyException(msg, cause);
    }

    public static void throwE(Throwable cause) {
        throw new DocsifyException(cause);
    }

    public static void throwE(int errCode, Throwable cause) {
        throw new DocsifyException(errCode, cause);
    }

    public static <T extends Throwable> boolean isInstanceof(T throwable) {
        return throwable instanceof DocsifyException;
    }

    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }
}